package service;

import com.example.excel.DataExcel;
import com.example.excel.DrawShapeCell;
import com.example.excel.DrawShapeColor;
import com.example.excel.PoiUtil;
import org.apache.poi.ss.usermodel.ShapeTypes;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * excel组件测试
 */
public class ExcelTest {

    public static void main(String[] args) {
        test2007();
        //test2003();
    }

    /**
     * 暂时不需要考虑2003
     * v10桌面版自带支持office2007
     * v5可安装wps2019，来支持office2007
     */
    private static void test2003() {
        String filePath = "test_write_excel2003.xlsx";
        List<DataExcel> rowDatas = createData();
        List<DrawShapeCell> drawShapeCellData = createDrawData();
        try {
            makeDir(extractFilePath(filePath), true);
            PoiUtil poiUtil = new PoiUtil(PoiUtil.VERSION_2003);
            poiUtil.write2003("sheetName", rowDatas);
            poiUtil.draw2003("sheetName", drawShapeCellData);
            boolean s = poiUtil.export(filePath);
            System.out.println(s);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void test2007() {
        String filePath = "test_write_excel.xlsx";
        List<DataExcel> rowDatas = createData();
        List<DrawShapeCell> drawShapeCellData = createDrawData();
        try {
            makeDir(extractFilePath(filePath), true);
            PoiUtil poiUtil = new PoiUtil(PoiUtil.VERSION_OVER_2007);
            poiUtil.write("sheetName", rowDatas);
            poiUtil.draw("sheetName", drawShapeCellData);
            boolean s = poiUtil.export(filePath);
            System.out.println(s);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String extractFilePath(String filePathName) {
        int nPos = filePathName.lastIndexOf('/');
        if (nPos < 0) {
            nPos = filePathName.lastIndexOf('\\');
        }
        return (nPos >= 0 ? filePathName.substring(0, nPos + 1) : "");
    }

    private static boolean makeDir(String _sDir, boolean bCreateParentDir) {
        boolean zResult = false;
        File file = new File(_sDir);
        if (bCreateParentDir) {
            zResult = file.mkdirs(); // 如果父目录不存在，则创建所有必需的父目录
        } else {
            zResult = file.mkdir(); // 如果父目录不存在，不做处理
        }
        if (!zResult) {
            zResult = file.exists();
        }
        return zResult;
    }

    private static List<DrawShapeCell> createDrawData() {
        List<DrawShapeCell> list = new ArrayList<>();
        //如果dy1>1则dy1-1且row1+1
        list.add(new DrawShapeCell("dy", ShapeTypes.RECT, new DrawShapeColor(255, 0, 0), 1.2, 0, 2.2, 0.66, 4, 4));
        list.add(new DrawShapeCell("dq", ShapeTypes.RECT, new DrawShapeColor(0, 255, 0), 2.5, 0.66, 3.5, 1.33, 4, 4));
        list.add(new DrawShapeCell("gd", ShapeTypes.RECT, new DrawShapeColor(211, 100, 255), 0, 1.33, 1, 2, 4, 4));
        list.add(new DrawShapeCell("chocker", ShapeTypes.RECT, new DrawShapeColor(211, 100, 200), 0, 1.33, 1, 2, 6, 5));
        return list;
    }

    private static List<DataExcel> createData() {
        List<DataExcel> dataExcelList = new ArrayList<>();
        dataExcelList.add(new DataExcel("test0912_2", 1, 1, 1, 10));
        dataExcelList.add(new DataExcel("日出时刻：", 1, 11, 1, 4));
        dataExcelList.add(new DataExcel("日落时刻：", 1, 15, 1, 4));
        dataExcelList.add(new DataExcel("序号", 2, 1, 2, 1));
        dataExcelList.add(new DataExcel("jh", 2, 2, 2, 1));
        dataExcelList.add(new DataExcel("lalala", 2, 3, 2, 1));
        dataExcelList.add(new DataExcel("09-12|09：00", 2, 4, 1, 6));
        dataExcelList.add(new DataExcel("09-12|10：00", 2, 10, 1, 6));
        dataExcelList.add(new DataExcel("09-12|11：00", 2, 16, 1, 6));
        dataExcelList.add(new DataExcel("09-12|12：00", 2, 22, 1, 6));
        for (int i = 0; i < 4; i++) {
            dataExcelList.add(new DataExcel("00", 3, i * 6 + 4));
            dataExcelList.add(new DataExcel("10", 3, i * 6 + 5));
            dataExcelList.add(new DataExcel("20", 3, i * 6 + 6));
            dataExcelList.add(new DataExcel("30", 3, i * 6 + 7));
            dataExcelList.add(new DataExcel("40", 3, i * 6 + 8));
            dataExcelList.add(new DataExcel("50", 3, i * 6 + 9));
        }
        for (int j = 1; j < 5; j++) {
            dataExcelList.add(new DataExcel(String.valueOf(j), 2 + j * 2, 1, 2, 1));
            dataExcelList.add(new DataExcel("100" + String.valueOf(j), 2 + j * 2, 2, 2, 1));
            for (int k = 0; k < 25; k++) {
                dataExcelList.add(new DataExcel("", 2 + j * 2, k + 3, 2, 1));
            }
        }
        dataExcelList.add(new DataExcel("xx值班员：", 12, 1, 3, 3));
        dataExcelList.add(new DataExcel("xx值班员：", 12, 4, 3, 3));
        dataExcelList.add(new DataExcel("xx值班员：", 15, 1, 3, 3));
        dataExcelList.add(new DataExcel("xx值班员：", 15, 4, 3, 3));
        return dataExcelList;
    }
}
